<template>
  <div>
    <p style="font-size: 15px;line-height: 60px">登录界面</p>
    <el-row>
      <el-col :span=7></el-col>
      <el-col :span=8>
        <el-form :model="form" label-width="120px">
          <el-form-item label="账户">
            <el-input v-model="form.id"/>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password"/>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span=9></el-col>
    </el-row>
    <el-radio-group v-model="form.type">
      <el-radio label="user">用户</el-radio>
      <el-radio label="admin">管理员</el-radio>
    </el-radio-group>
    <el-row></el-row>
    <p></p>
    <el-button type="primary" @click="onSubmit">登录</el-button>
    <el-button @click="goHome">返回</el-button>
  </div>
</template>

<script>
import axios from 'axios';
import Global from '../components/Global.vue';

export default {
  name: 'LoginView',
  data() {
    return {
      form: {
        id: '',
        password: '',
        type: 'user',
      },
    };
  },
  methods: {
    onSubmit() {
      const path = '/api/login';
      axios.post(path, this.form)
        .then((res) => {
          if (res.data.ok) {
            Global.id = this.form.id;
            Global.type = this.form.type;
            this.$message.success('登录成功');
            this.$router.replace(`/${this.form.type}`);
          } else {
            this.$message.success('登录失败');
            // this.$router.replace('/');
          }
        })
        .catch((error) => {
          // eslint-disable-next-line
          console.error(error);
        });
    },
    goHome() {
      this.$router.replace('/');
    },
  },
};
</script>

<style scoped>

</style>
